/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jauker.badgeview.example.view;

import ohos.aafwk.ability.Ability;
import ohos.agp.colors.RgbColor;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import com.jauker.badgeview.example.ResourceTable;
import com.jauker.widget.BadgeView;

import java.util.List;

public class SamplePageView extends AbstractPageView implements Component.ClickedListener {
    public Context context;
    public Ability abilitySlice;
    private BadgeView backgroundDrawableBadge;
    private BadgeView backgroundShapeBadge;
    private BadgeView backgroundDefaultBadge;
    private BadgeView counterBadge;
    private BadgeView gravityBadge;
    private BadgeView textStyleBadge;
    private BadgeView visibilityBadgeView;
    private PixelMap imagePixelMap;
    private List<? extends PageInfo> pageViews;
    private List<AbstractPageView> mPageViews;

    public SamplePageView(Ability abilitySlice, String name, Color txtColor) {
        super(abilitySlice, name, txtColor);
        this.abilitySlice = abilitySlice;
    }

    public SamplePageView(Ability abilitySlice, String name, int iconId, int iconIdSelected, Color txtColor) {
        super(abilitySlice, name, iconId, iconIdSelected, txtColor);
    }

    @Override
    public void initView() {
        super.setRootView(loadView());
    }

    private Component loadView() {
        ComponentContainer layout = (ComponentContainer) LayoutScatter.getInstance(super.getSlice()).
                parse(ResourceTable.Layout_ability_main, null, false);
        Component backgroundDefaultView = layout.findComponentById(ResourceTable.Id_backgroundDefault);
        backgroundDefaultBadge = new BadgeView(abilitySlice);
        backgroundDefaultBadge.setBadgeCount(42);
        backgroundDefaultBadge.setTextSize(40);
        backgroundDefaultBadge.setBackground(12, RgbPalette.parse("#9b2eef"));
        backgroundDefaultBadge.setTargetView(backgroundDefaultView);

        Component backgroundDrawableView = layout.findComponentById(ResourceTable.Id_backgroundDrawable);
        backgroundDrawableBadge = new BadgeView(abilitySlice);
        backgroundDrawableBadge.setBadgeCount(88);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShape(ShapeElement.RECTANGLE);
        shapeElement.setRgbColor(new RgbColor(0x1000002));
        backgroundDrawableBadge.setBackground(shapeElement);
        backgroundDrawableBadge.setTextSize(40);
        backgroundDrawableBadge.setBackground(12, RgbPalette.parse("#9b2eef"));
        backgroundDrawableBadge.setTargetView(backgroundDrawableView);

        Component backgroundShapeView = layout.findComponentById(ResourceTable.Id_backgroundShape);
        backgroundShapeBadge = new BadgeView(abilitySlice);
        backgroundShapeBadge.setBadgeCount(47);
        backgroundShapeBadge.setTextSize(40);
        backgroundShapeBadge.setBackground(12, RgbPalette.parse("#9b2eef"));
        backgroundShapeBadge.setTargetView(backgroundShapeView);

        Component counterView = layout.findComponentById(ResourceTable.Id_counter);
        counterView.setClickedListener(this);
        counterBadge = new BadgeView(abilitySlice);
        counterBadge.setBadgeCount(-2);
        counterBadge.setTextSize(40);
        counterBadge.setBackground(12, RgbPalette.parse("#9b2eef"));
        counterBadge.setTargetView(counterView);

        Component gravityView = layout.findComponentById(ResourceTable.Id_gravity);
        gravityView.setClickedListener(this);
        gravityBadge = new BadgeView(abilitySlice);
        gravityBadge.setBadgeGravity(LayoutAlignment.START | LayoutAlignment.BOTTOM);
        gravityBadge.setBadgeCount(4);
        gravityBadge.setTextSize(40);
        gravityBadge.setBackground(12, RgbPalette.parse("#9b2eef"));
        gravityBadge.setTargetView(gravityView);

        Component textStyleView = layout.findComponentById(ResourceTable.Id_textStyle);
        textStyleView.setClickedListener(this);
        textStyleBadge = new BadgeView(abilitySlice);
        textStyleBadge.setBadgeCount(18);
        textStyleBadge.setTextSize(40);
        textStyleBadge.setBackground(12, RgbPalette.parse("#9b2eef"));
        textStyleBadge.setTargetView(textStyleView);
        textStyleBadge.setFont(Font.SANS_SERIF);

        Component visibilityView = layout.findComponentById(ResourceTable.Id_visibility);
        visibilityView.setClickedListener(this);
        visibilityBadgeView = new BadgeView(abilitySlice);
        visibilityBadgeView.setBadgeCount(1);
        visibilityBadgeView.setTextSize(40);
        visibilityBadgeView.setBackground(12, RgbPalette.parse("#9b2eef"));
        visibilityBadgeView.setTargetView(visibilityView);
        return layout;
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_counter: {
                counterBadge.incrementBadgeCount(1);
                break;
            }
            case ResourceTable.Id_gravity: {
                gravityBadge.incrementBadgeCount(1);
                if (gravityBadge.getBadgeCount() % 9 == 0) {
                    gravityBadge.setBadgeGravity(LayoutAlignment.LEFT | LayoutAlignment.TOP);
                } else if (gravityBadge.getBadgeCount() % 9 == 1) {
                    gravityBadge.setBadgeGravity(LayoutAlignment.RIGHT | LayoutAlignment.BOTTOM);
                } else if (gravityBadge.getBadgeCount() % 9 == 2) {
                    gravityBadge.setBadgeGravity(LayoutAlignment.LEFT | LayoutAlignment.BOTTOM);
                } else if (gravityBadge.getBadgeCount() % 9 == 3) {
                        gravityBadge.setBadgeGravity(LayoutAlignment.RIGHT | LayoutAlignment.TOP);
                } else if (gravityBadge.getBadgeCount() % 9 == 4) {
                    gravityBadge.setBadgeGravity(LayoutAlignment.LEFT | LayoutAlignment.VERTICAL_CENTER);
                } else if (gravityBadge.getBadgeCount() % 9 == 5) {
                    gravityBadge.setBadgeGravity(LayoutAlignment.RIGHT | LayoutAlignment.VERTICAL_CENTER);
                } else if (gravityBadge.getBadgeCount() % 9 == 6) {
                    gravityBadge.setBadgeGravity(LayoutAlignment.CENTER);
                } else if (gravityBadge.getBadgeCount() % 9 == 7) {
                    gravityBadge.setBadgeGravity(LayoutAlignment.HORIZONTAL_CENTER | LayoutAlignment.TOP);
                } else if (gravityBadge.getBadgeCount() % 9 == 8) {
                    gravityBadge.setBadgeGravity(LayoutAlignment.HORIZONTAL_CENTER | LayoutAlignment.BOTTOM);
                }
                break;
            }
            case ResourceTable.Id_visibility: {
                visibilityBadgeView.setVisibility(visibilityBadgeView.getVisibility() == Component.INVISIBLE ? Component.VISIBLE : Component.INVISIBLE);
                break;
            }
            default:
                break;
        }
    }

}
